﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Documents;
using System.Windows.Navigation;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Protocal.App.Common;
using 多协议通信平台.Common;
using 多协议通信平台.Entity;
using 多协议通信平台.Models;
using 多协议通信平台.Views;

namespace 多协议通信平台.ViewModels.Login
{
    public partial class LoginViewModel : ObservableObject, IDataErrorInfo
    {
        [ObservableProperty]
        private string account;

        [ObservableProperty]
        private string password;

        [ObservableProperty]
        private bool rememberPass;

        [ObservableProperty]
        private bool showLoading = false;

        private Data_LocalRecord record;
        public LoginViewModel()
        {
            InitLoad();
        }

        private void InitLoad()
        {
            IService.IBaseService service = IocHelper.GetService();
            Data_LocalRecord data_LocalRecord = service.SelectOne<Data_LocalRecord>(null);
            if (data_LocalRecord != null)
            {
                record = data_LocalRecord;
                Account = record.Account;
                Password = EncryptHelper.AesDecrypt(record.Password);
                RememberPass = record.RememberPwd;
            }
        }

        // 用来避免首次编辑前绑定验证的问题
        private int count = 0;

        public string this[string columnName]
        {
            get
            {
                if(count < 2)
                {
                    count++;
                    return "";
                }
                switch (columnName)
                {
                    case "Account":
                        if (string.IsNullOrEmpty(Account))
                        {
                            Error = "账号不能为空";
                            return Error;
                        }
                        else if (Account.Length < 4)
                        {
                            Error = "账号长度不能小于四";
                            return Error;
                        }
                        break;
                    case "Password":
                        string regStr = @"/^.*(?=.{12,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[~!@\.#$^&*? ]).*/";
                        if (string.IsNullOrEmpty(Password))
                            Error = "密码不能为空";
                        else if (!Regex.Match(Password, regStr).Success)
                        {
                            Error = "密码必须大于12位，并且包含数字、特殊符号和中英文大小写";
                        }
                        break;
                    default:
                        break;
                }
                return Error;
            }
        }

        public string Error { get;set; }

        [RelayCommand]
        public void DoLogin(object obj)
        {
            try
            {
                ShowLoading = true;
                // 登录校验

                // 获取登录信息
                UserModel userModel = new UserModel()
                {
                    Id = 1,
                    Avatar = "pack://siteofOrigin:,,,/Assets/Avatars/a14.jpg",
                    Username = "贾詩涵",
                    Account = "admin",
                    LastLogin = DateTime.Now,
                };

                // 设置本地上下文
                if (RememberPass)
                {
                    Data_LocalRecord localRecord = new Data_LocalRecord()
                    {
                        Id = 1,
                        Username = userModel.Username,
                        Account = Account,
                        Password = EncryptHelper.AesEncrypt(Password),
                        RememberPwd = RememberPass
                    };
                    IService.IBaseService service = IocHelper.GetService();
                    if (record == null)
                    {
                        service.Insert<Data_LocalRecord>(localRecord);
                    }
                    else
                    {
                        service.Update(localRecord);
                    }
                }
                ContextHelper.Set("user", userModel);

                LoginView? loginView = obj as LoginView;
                loginView.DialogResult = true;

            }
            finally
            {
                showLoading = false;
            }
        }
    }
}
